package com.googlecode.objectify.generic;

import com.google.appengine.api.datastore.AsyncDatastoreService;
import com.google.appengine.api.datastore.DatastoreAttributes;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Index;
import com.google.appengine.api.datastore.Index.IndexState;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyRange;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.TransactionOptions;
import static com.googlecode.objectify.generic.FutureHelper.quietGet;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;

/**
 *
 * @author Secondary User
 */
@RequiredArgsConstructor
public class DatastoreServiceAdaptor implements DatastoreService
{

	private final AsyncDatastoreService async;
	private final IdRangeAllocator rangeAllocator;

	@Override
	public KeyRange allocateIds(Key parent, String kind, long num)
	{
		return quietGet(async.allocateIds(parent, kind, num));
	}

	@Override
	public KeyRange allocateIds(String kind, long num)
	{
		return quietGet(async.allocateIds(kind, num));
	}

	@Override
	public KeyRangeState allocateIdRange(KeyRange range)
	{
		return rangeAllocator.allocateIdRange(range);
	}

	@Override
	public DatastoreAttributes getDatastoreAttributes()
	{
		return quietGet(async.getDatastoreAttributes());
	}

	@Override
	public Transaction beginTransaction(TransactionOptions options)
	{
		return quietGet(async.beginTransaction(options));
	}

	@Override
	public Transaction beginTransaction()
	{
		return quietGet(async.beginTransaction());
	}

	@Override
	public void delete(Key... keys)
	{
		quietGet(async.delete(keys));
	}

	@Override
	public void delete(Iterable<Key> keys)
	{
		quietGet(async.delete(keys));
	}

	@Override
	public void delete(Transaction txn, Key... keys)
	{
		quietGet(async.delete(txn, keys));
	}

	@Override
	public void delete(Transaction txn, Iterable<Key> keys)
	{
		quietGet(async.delete(txn, keys));
	}

	@Override
	public Entity get(Key key) throws EntityNotFoundException
	{
		//TODO: Check if this throws EntityNotFoundException
		return quietGet(async.get(key));
	}

	@Override
	public Map<Key, Entity> get(Iterable<Key> keys)
	{
		return quietGet(async.get(keys));
	}

	@Override
	public Entity get(Transaction txn, Key key) throws EntityNotFoundException
	{
		//TODO: Check if this throws EntityNotFoundException
		return quietGet(async.get(txn, key));
	}

	@Override
	public Map<Key, Entity> get(Transaction txn, Iterable<Key> keys)
	{
		return quietGet(async.get(txn, keys));
	}

	@Override
	public Collection<Transaction> getActiveTransactions()
	{
		return async.getActiveTransactions();
	}

	@Override
	public Transaction getCurrentTransaction()
	{
		return async.getCurrentTransaction();
	}

	@Override
	public Transaction getCurrentTransaction(Transaction txn)
	{
		return async.getCurrentTransaction(txn);
	}

	@Override
	public PreparedQuery prepare(Query query)
	{
		return async.prepare(query);
	}

	@Override
	public PreparedQuery prepare(Transaction txn, Query query)
	{
		return async.prepare(txn, query);
	}

	@Override
	public Key put(Entity entity)
	{
		return quietGet(async.put(entity));
	}

	@Override
	public List<Key> put(Iterable<Entity> entities)
	{
		return quietGet(async.put(entities));
	}

	@Override
	public Key put(Transaction txn, Entity entity)
	{
		return quietGet(async.put(txn, entity));
	}

	@Override
	public List<Key> put(Transaction txn, Iterable<Entity> entities)
	{
		return quietGet(async.put(txn, entities));
	}

	@Override
	public Map<Index, IndexState> getIndexes()
	{
		return quietGet(async.getIndexes());
	}
}
